<?php
    include '../connectdb.php';
    if (isset($_POST['action'])) {
        $total = 0;
        foreach ($_POST as $key => $val) {
            if (strpos($key, "sInput") !== false) {
                list($tmp, $sid) = explode('-', $key);
                $q = 'insert into booking_service(booking_id, service_id, price) values('.$_POST['booking_id'].', '.$sid.', '.$val.');';
                mysqli_query($link, $q);
                $total+= $val;
            }
        }
        $q = "update booking set total_fee=".$total.", checkout_date='".date('Y-m-d H:i:s')."' where booking_id=".$_POST['booking_id'].";";
        mysqli_query($link, $q);
        $q = "update booking_detail set status_id=4 where booking_id=".$_POST['booking_id'];
        mysqli_query($link, $q);
        header('Location: main.php?checkout=true');
        die;
    }
    $q = "select booking.*, customer.customer_fname, customer.customer_lname from booking, customer where booking.customer_id=customer.customer_id and booking.booking_id=".$_POST['booking_id'];
    $result = mysqli_query($link, $q);
    $row = mysqli_fetch_array($result);
    $total = $row['total_price'];
    $deposit = $row['deposit'];
    $remain = $total - $deposit;
    $q = "select min(booking_detail.date) as mindate, max(booking_detail.date) as maxdate, status_id from booking_detail where booking_id=".$_POST['booking_id'];
    $res = mysqli_query($link, $q);
    $r = mysqli_fetch_array($res);
    $action = 'edit';
    $cod = date ("Y-m-d", strtotime("+1 day", strtotime($r['maxdate'])));
    $status = $r['status_id'];
?>
<form action="main.php?do_checkout=true" method="POST">
<input type="hidden" name="booking_id" value="<?php echo $_POST["booking_id"] ?>" />
<input type="hidden" name="action" value="do_checkout" />
<table border="1" width="764">
    <tr>
        <td colspan="2" align="center"><h3>Booking Management</h3></td>
    </tr>
    <tr>
        <td>id</td>
        <td><?php echo $row['booking_id'] ?></td>
    </tr>
    <tr>
        <td>Customer</td>
        <td><?php echo $row['customer_fname'].' '.$row['customer_lname'] ?></td>
    </tr>
    <tr>
        <td>Booking Date</td>
        <td><?php echo $row['booking_date'] ?></td>
    </tr>
    <tr>
        <td>Check-in</td>
        <td><?php echo $r['mindate'] ?></td>
    </tr>
    <tr>
        <td>Check-out</td>
        <td><?php echo $cod ?></td>
    </tr>
    <tr>
        <td>Total</td>
        <td><?php echo $total ?></td>
    </tr>
    <tr>
        <td>Paid</td>
        <td><?php echo $deposit ?></td>
    </tr>
    <tr>
        <td>Remain</td>
        <td><?php echo $remain ?></td>
    </tr>
    <tr>
        <td>Paid Date</td>
        <td><?php echo $row['deposit_date'] ?></td>
    </tr>
    <tr>
        <td colspan="2"> -- addition fee</td>
    </tr>
    <?php
        $q = "select * from service_type order by service_id";
        $sRes = mysqli_query($link, $q);
        $total = 0;
        while ($row = mysqli_fetch_array($sRes)) {
            $fee = ($_POST["sInput-".$row['service_id']] != '') ? $_POST["sInput-".$row['service_id']] : 0;
            $total+= $fee;
    ?>
    <tr>
        <td><?php echo $row['service_name'] ?></td>
        <td>
            <input type="hidden" name="sInput-<?php echo $row['service_id'] ?>" value="<?php echo $fee ?>">
            <?php echo $fee ?>
        </td>
    </tr>
    <?php
        }
    ?>
    <tr>
        <td><strong>Total fee</strong></td>
        <td><?php echo $total ?></td>
    </tr>
    <tr>
        <td><strong>Total Remaining Cost</strong></td>
        <td><?php echo $total+$remain ?></td>
    </tr>
    <tr>
        <td colspan="2" align="center"><input type="submit" value="Check-out" /> <input type="reset" value="reset"/></td>
    </tr>
</table>
</form>
